#!/bin/bash

# To use the xrandr extension you may need to:
# sudo apt-get install libxrandr-dev
SOURCE=(
	glyph.cpp
	app_yeee754.cpp
)
COMPILE_ARGS=(
	-o yeee754
)
LINK_ARGS=(
	-lrt
	-lm
	-lX11
)

case `uname -m` in

armv7l)
	SOURCE+=(
		main.cpp
		amd_perf_monitor.cpp
	)
	COMPILE_ARGS+=(
		-marm
		-mcpu=cortex-a8
		-mfpu=neon
		-D_LINUX
		-DEGL_EGLEXT_PROTOTYPES
		-DGL_GLEXT_PROTOTYPES
	)
	LINK_ARGS+=(
		-ldl
		-lGLESv2
		-lEGL
	)
	;;
x86_64)
	SOURCE+=(
		main_glx.cpp
	)
	COMPILE_ARGS+=(
		-msse3
		-mfpmath=sse
		-DPLATFORM_GLX
		-DGLX_GLXEXT_PROTOTYPES
		-DGLCOREARB_PROTOTYPES
		-DGL_GLEXT_PROTOTYPES
	)
	LINK_ARGS+=(
		-lGL
	)
	;;
*)
	echo "unknown target platform; abort"
	exit 254
esac

COMPILE_ARGS+=( -fno-rtti -ffast-math -fstrict-aliasing )
OPT_COMPILE_ARGS=( -O3 -funroll-loops -DNDEBUG )

declare -a EXT_COMPILE_ARGS

args=("$@")

for (( i=0; i < $#; i++)); do

	case ${args[$i]} in

	debug)
		OPT_COMPILE_ARGS=(
			-Wall
			-O0
			-g
			-DDEBUG
		)
		;;
	xrandr)
		EXT_COMPILE_ARGS+=(
			-DUSE_XRANDR
		)
		LINK_ARGS+=(
			-lXrandr
		)
		;;
	*)
		echo "usage: $0 [debug] [xrandr]"
		exit 255
	esac

done

BUILD_CMD="g++ "${SOURCE[@]}" "${COMPILE_ARGS[@]}" "${OPT_COMPILE_ARGS[@]}" "${EXT_COMPILE_ARGS[@]}" "${LINK_ARGS[@]}
echo $BUILD_CMD
$BUILD_CMD
